package com.sxj.features.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import lombok.Synchronized;

/**
 * @author Sun Xiaojie
 */
public class Yeild {
	static int i = 0;
	
	public static void main(String[] args) {
		
//		ExecutorService executorService = new ThreadPoolExecutor(2, 3, 
//				1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000));
//		executorService.submit(new Thread(){
//				@Override
//				public void run() {
//					System.out.println("B");
//					add();
//				}
//			});
		
			Thread thread1 = new Thread(){
				@Override
				public void run() {
					while(i<=20) {
						if (i%2==0) {
							System.out.println("A");
							add();	
						}
					}
				}
			};
			thread1.start();
//			thread1.yield();
			Thread thread2 = new Thread(){
				@Override
				public void run() {
					while(i<=20) {
						if(i%2==1) {
							System.out.println("B");
							add();	
						}
					}
				}
			};
			thread2.start();
//			thread2.yield();
		}
//		Runnable thread = getThread();
//		thread.run();
		
		
//	}
	
	@Synchronized
	static
	void add(){
		i++;
	}
	
	private static Runnable getThread(){
		return () -> {
			System.out.println("A");
		};
	}

}
